#include "dso_const.h"
#include "plugin.h"
#include "plugin_def.h"

#include "micro.h"

//#include "geek.h"

// main entry, exit, and query functions
// do not alter any other files but this one

uint16 smallIcon[] = { 9, 9, //width, height
	31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 
	31, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 31, 
	31, 32767, 31744, 31744, 31744, 31744, 31744, 31744, 32767, 31, 
	31, 32767, 31744, 32767, 32767, 32767, 32767, 31744, 32767, 31, 
	31, 32767, 31744, 32767, 512, 512, 32767, 31744, 32767, 31, 
	31, 32767, 31744, 32767, 512, 512, 32767, 31744, 32767, 31, 
	31, 32767, 31744, 32767, 32767, 32767, 32767, 31744, 32767, 31, 
	31, 32767, 31744, 31744, 31744, 31744, 31744, 31744, 32767, 31, 
	31, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767, 31, 
	31, 31, 31, 31, 31, 31, 31, 31, 31, 31};

// called just after loading, before showing DSO home screen
// possible to display a splash screen here if you so desire
// fat is init'd at this point
void DSO_LoadLibrary(void) 
{

}

// called under certain circumstances to destroy the plugin
void DSO_FreeLibrary(void)
{

}

FAT_FILE *XF;


void readByte(unsigned char *data) {
	char str[32];
	unsigned char c;
	
	FAT_fread(&c, 1, 1, XF);
	*data = c;

//	sprintf(str, ":%02X", c);
//	debugPrint(str);
//	writeDebug(str);

}


// called when you open the file.  if you dont provide this function in queryinterface, (a) open becomes (a) N/A in the browser
void launchFile(char *path)
{	
	int res;
	char str[32];

	libfb_setColor(0);
	swiWaitForVBlank();
	libfb_swapBuffers();



	//


//	swiDelay(100000);	// SYNC
//	wr(0xFF);
//	wr(0xFF);
//	wr(0xFF);
//	wr(0xFF);
//	wr(0xFF);
//	swiDelay(100000);
//	wr_gpio_out(0x2E);
//	wr_gpio_dir(0x2B);


	res = 999;	
		
	while(1)
	{
		scanKeys();
		
		if(keysDown() & KEY_A)
		{
			while(!(keysUp() & KEY_A))
			{			
				scanKeys();
				swiWaitForVBlank();
			}



			// Try open file
			XF = FAT_fopen( path, "r" );
	writeDebug("EXEC");
			res = xsvfExecute();
			FAT_fclose(XF);
		}

		if(keysDown() & KEY_B)
		{
			while(!(keysUp() & KEY_B))
			{			
				scanKeys();
				swiWaitForVBlank();
			}
			return;
		}

			
		libfb_setFont(FONT_ARIAL_11);
		libfb_dispString(SCREEN1, 5, 5, "XSVF File:");

		libfb_setFont(FONT_ARIAL_9);
		libfb_dispString(SCREEN1, 5, 20, path);

		sprintf(str, "= %08X", res);
		libfb_dispString(SCREEN1, 5, 35, str);		

	
		drawButtonTexts(BUTTON_A, "Execute", BUTTON_B, "Go Back");
		
		swiWaitForVBlank();
		libfb_swapBuffers();
	}	
}

// called when you open the file with alternate load.  if you dont provide this function in the queryinterface, (y) open doesn't appear
void launchFileAlt(char *path)
{
	loadSound("/music.mod");
	launchFile(path);
	closeSound();
}

// return proper plugin information here, see dso_const.h for information
// any of these can be safely left out
u32 DSO_QueryInterface(long wInterface, long wData)
{
	switch(wInterface)
	{
		case REQ_TYPE: 
			return PLUGIN_FILEFORMAT;
		case REQ_EXTLIST:
			return (u32)"xsvf"; // as many file formats as you want, comma separated
		case REQ_EXTDESC:
			return (u32)"Xilinx XSVF File"; // generic file description
		case REQ_SMALLICON:
			return (u32)smallIcon; // small icon graphic, libfb normal or compressed format
		case REQ_LONGDESC:
			return (u32)"Execute and play\na Xilinx XSVF File\nusing default JTAG port/driver";
		case REQ_LARGEICON:
			return (u32)0;
		case REQ_LOADFILE:
			return (u32)launchFile; // return null to make dso disable the primary launch
		case REQ_ALTLOADFILE:
			return (u32)launchFileAlt; // return null to make dso disable the secondary launch
		case REQ_LOADSTRING:
			return (u32)"Play XSVF File"; // return null to force dso to use default string
		case REQ_ALTLOADSTRING:
			return (u32)"Do More Nothing!"; // return null to force dso to use default string
		default:
			return 0;
	}
	
	return 0; // make some compilers happy
}

